/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package shaman.storage.impl.file;

import java.util.Map;
import shaman.storage.common.FileProperties;
import shaman.storage.common.FilePropertiesAccessor;

/**
 *
 * @author dmitriy
 */
public class StoredFileProperties extends FileProperties {

    public static final String STORED_PROPERTIES_PREFIX="FileBackup::StoredFile::";
    public static final String PROPERTY_BLOCK_OFFSET=STORED_PROPERTIES_PREFIX+"blockOffset";
    public static final String PROPERTY_FILE_OFFSET=STORED_PROPERTIES_PREFIX+"fileOffset";
    public static final String PROPERTY_FILE_LENGTH=STORED_PROPERTIES_PREFIX+"fileLength";

    public StoredFileProperties(String filename, Map<String, String> properties)
    {
        super(filename, properties);
    }

    public StoredFileProperties(FilePropertiesAccessor acc) {
        super(acc);
    }


    public long getBlockOffset() {
        return getLongProperty(PROPERTY_BLOCK_OFFSET);
    }

    public void setBlockOffset(long offset) {
        setLongProperty(PROPERTY_BLOCK_OFFSET, offset);
    }

    public long getFileOffset() {
        return getLongProperty(PROPERTY_FILE_OFFSET);
    }

    public void setFileOffset(long offset) {
        setLongProperty(PROPERTY_FILE_OFFSET, offset);
    }

    public long getFileLength() {
        return getLongProperty(PROPERTY_FILE_LENGTH);
    }

    public void setFileLength(long length) {
        setLongProperty(PROPERTY_FILE_LENGTH, length);
    }

    private void setLongProperty(String name, long value)
    {
        setProperty(name, String.valueOf(value));
    }

    private long getLongProperty(String name) {
        String valueString=getProperty(name);
        if (valueString==null)
            throw new NullPointerException("Missing stored file metadata");
        long value;
        try {
            value=Long.parseLong(valueString);
        } catch (NumberFormatException ex) {
            throw new NullPointerException("Invalid stored file metadata");
        }
        return value;
    }

}
